import defaultData from "./data";
import Props from "../base/Props";
import { Row } from 'antd';
import { Fragment } from "react";
import { WeaInputNumber, WeaCheckbox, WeaSelect, WeaLocaleProvider, WeaInput } from 'ecCom';
import NewInputNumber from '../../../../common/InputNumber';
import './Props.less';

const { getLabel } = WeaLocaleProvider;
const selectOpts = [
  { key: '1', showname: getLabel(526733,'整数') },
  { key: '2', showname: getLabel(526734,'浮点数') },
];
export default class NumberProps extends Props {

  refsPrecision = isPrecision => {
    const { precision } = this.props;
    const newPrecision = precision ? precision : '2';

    if(!isPrecision) return;

    return {
      precision: newPrecision
    };

  }

  renderDefaultValue() {
    const { data: { precision, isPrecision, type, defaultValue } } = this.props;
    const prec = Math.min(precision || 0, 6);
    const isFloat = type == '1';
    const isPrecisonChecked = Boolean(Number(isPrecision));
    const step = isFloat ? 1 : isPrecisonChecked ? Math.pow(10, -prec).toFixed(prec) : Math.pow(10, -6).toFixed(6);

    return (
      <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@e96iwu`} className='wea-edc-number-defvalue'>
        <label>{getLabel(502451,'默认值')}</label>
        <NewInputNumber ecId={`${this && this.props && this.props.ecId || ''}_NewInputNumber@cvsfnh`}
          key={defaultValue} 
          defaultValue={defaultValue} 
          onBlur={this.handlePropChange('defaultValue')} 
          step={step} 
          min={-999999999} 
          max={999999999}
          type='number'
        />
      </Row>
    );
  }

  renderContent () {
    const { data: { precision, isPrecision, type }, field } = this.props;
    const { fieldId } = field;

    const readOnly = fieldId && fieldId > 0;
    return (
      <Fragment ecId={`${this && this.props && this.props.ecId || ''}_Fragment@0nr13r`}>
        <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@jptne4`} className='edc-sepline edc-row-hor'>
          <label>{getLabel(526998,'类型')}</label>
          <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@2s2kk7`}
            disabled={readOnly}
            value={type || (readOnly ? '2' : '1')}
            options={selectOpts}
            style={{ width: '100%' }}
            onChange={this.handlePropChange('type')}
          />
        </Row>
        {
          (type && type == '2') &&
          <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@jabki7`}>
            <div className={readOnly ? 'field-readonly' : ''}>
              <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@p52mb4`}
                value={isPrecision}
                content={<span>{getLabel(526999,'不超过')}</span>}
                onChange={this.handlePropChange("isPrecision:(refs=refsPrecision)")}
                disabled={readOnly}
              />
              <WeaInputNumber ecId={`${this && this.props && this.props.ecId || ''}_WeaInputNumber@j3yx1l`} 
                value={precision} min={1} max={6} 
                style={{ width: 65, 'margin-right': 5 }} 
                onChange={this.handlePropChange('precision')}
                viewAttr={readOnly ? 1: 2}
              />
              {getLabel(527017,'位小数')}
            </div>
          </Row>
        }

      </Fragment>
    );
  }
}

NumberProps.defaultProps = {
  data: defaultData
};